<?php
/**
 * OrganizationAccessController class file.
 *
 * @package   controllers
 * @since     1.0
 * @filesource
 */

/**
 * Controller for the Organization-Access. Contains actions to manage organization access that is assigned to users.
 * Controller class is also responsible for rendering page.
 */
class OrganizationAccessController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * Return a list of filters that will be used to control access.
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter which is declared in $this->filters().
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
		array('allow',  // only allow admin to perform all actions
					'actions'=>array('admin','create','update','delete'),
					'roles'=>array('admin'),
		),
		array('deny',  // deny all other users
					'users'=>array('*'),
		),
		);
	}

	/**
	 * Creates a new organization access.
	 * This action will display a form for creating a organization access if no data posted back.
	 * If data posted back, it will validate the data and try to save it to database.
	 * If creation is successful, the browser will be redirected to the 'admin' page.
	 * @param integer id of user for whom the organization access is created.
	 */
	public function actionCreate($userId)
	{
		$model=new OrganizationAccess;
		
		$userId=intval($userId);
		$model->userId=$userId;//make sure new record related to given user.
		$model->user = User::model()->findByPk($userId);//Got info of given user to render page.

		if(isset($_POST['OrganizationAccess']))
		{
			$model->attributes=$_POST['OrganizationAccess'];
			if($model->save())
				$this->redirect(array('admin','userId'=>$model->userId));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular organization access.
	 * This action will display a form for updating a organization access if no data posted back.
	 * If data posted back, it will validate the data and try to save it to database.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the organization access to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['OrganizationAccess']))
		{
			$model->attributes=$_POST['OrganizationAccess'];
			if($model->save())
				$this->redirect(array('admin','userId'=>$model->userId));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular organization access.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the organization access to be deleted
	 * @throws CHttpException if the request is not a POST request.
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}


	/**
	 * Manages all organization accesses of a given user.
	 * This action will render an admin page using OrganizationAccess's method search() to produce a list of organization accesses.
	 * This method will produce a list of organizations accesses by search conditions.
	 * @param integer id of user whose organiztion accesses will be displayed.
	 */
	public function actionAdmin($userId)
	{
		$model=new OrganizationAccess('search');
		$model->unsetAttributes();  // clear any default values
		
		$model->userId=intval($userId);//make sure all results related to the given user.
		$model->user = User::model()->findByPk($userId);//Got info of given user to render page.
		
		if(isset($_GET['OrganizationAccess']))
			$model->attributes=$_GET['OrganizationAccess'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the OrganizationAccess object based on the primary key given in the GET variable.
	 * @param integer the ID of the organization access to be loaded
	 * @return OrganizationAccess
	 * @throws CHttpException If the organization access is not found
	 */
	public function loadModel($id)
	{
		$model=OrganizationAccess::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}